package com.example.bus.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.bus.entity.BusLine;
import com.example.bus.service.BusLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/busline")
public class BusLineController {
    @Autowired
    private BusLineService busLineService;
    // 获取所有公交线路
    @GetMapping("/list")
    public List<BusLine> getAllBusLines() {
        return busLineService.list();
    }
    // 通过线路名查询公交线路
    @GetMapping("/name/{lineName}")
    public BusLine getBusLineByName(@PathVariable("lineName") String lineName) {
        return busLineService.getOne(new QueryWrapper<BusLine>().eq("line_name", lineName));
    }
    // 通过线路号查询公交线路
    @GetMapping("/number/{lineNumber}")
    public BusLine getBusLineByNumber(@PathVariable("lineNumber") int lineNumber) {
        return busLineService.getOne(new QueryWrapper<BusLine>().eq("line_number", lineNumber));
    }
    // 增加公交线路
    @PostMapping("/add")
    public String addBusLine(@RequestBody BusLine busLine) {
        busLineService.save(busLine);
        return "Bus line added successfully";
    }
    // 更新公交线路
    @PutMapping("/update/{id}")
    public String updateBusLine(@PathVariable("id") Long id, @RequestBody BusLine busLine) {
        busLine.setId(id); // 设置ID
        busLineService.updateById(busLine); // 调用更新方法
        return "Bus line updated successfully";
    }
    // 删除公交线路
    @DeleteMapping("/delete/{id}")
    public String deleteBusLine(@PathVariable("id") Long id) {
        busLineService.removeById(id);
        return "Bus line deleted successfully";
    }


}

